import os,django

os.environ.setdefault ('DJANGO_SETTINGs_MODULE','atl_project.settings')

if django.VERSION >= (1,7):#自动判断版本
    django.setup()
    
list = []
other = []
filepath = r'\\192.0.7.1\驱动'
    
def find_list (filepath):
    files = os.listdir (filepath) # 遍历filepath下所有文件，包括子目录
    for i in files:
        fi_d = os.path.join (filepath,i) # filepath （主目录路径）+ i（子文件名称）= 子文件路径
        # print (fi_d)
        if os.path.isdir (fi_d): # 判断是否为文件夹
            file = i.lower ()
            if 'hp' in file and len (file) < 25:
                f = os.listdir (fi_d)
                for s in f : 
                    fi_s = os.path.join (fi_d,s)
                    # print (fi_s)
                    if os.path.isdir (fi_s): # 判断是否为文件夹
                        list.append (('HP驱动',s,fi_d))
                    else:
                        list.append (('HP驱动',i,fi_d))
                        
            elif 'dell' in file and len (file) < 25:
                f = os.listdir (fi_d)
                for s in f: 
                    fi_s = os.path.join (fi_d,s)
                    # print (fi_s)
                    if os.path.isdir (fi_s): # 判断是否为文件夹
                        list.append (('DELL驱动',s,fi_d))
                    else:
                        list.append (('DELL驱动',i,fi_d))
                
            elif 'lenovo' in file and len (file) < 25:
                f = os.listdir (fi_d)
                for s in f: 
                    fi_s = os.path.join (fi_d,s)
                    # print (fi_s)
                    if os.path.isdir (fi_s): # 判断是否为文件夹
                        list.append (('Lenovo驱动',s,fi_d))
                    else:
                        list.append (('Lenovo驱动',i,fi_d))
            
            elif 'huawei' in file and len (file) < 25:
                f = os.listdir (fi_d)
                for s in f: 
                    fi_s = os.path.join (fi_d,s)
                    # print (fi_s)
                    if os.path.isdir (fi_s): # 判断是否为文件夹
                        list.append (('Huawei驱动',s,fi_d))
                    else:
                        list.append (('Huawei驱动',i,fi_d))
                        
            else:
                find_list (fi_d)
               
    # print (set(list))
    
def find_other (filepath):
    files = os.listdir (filepath) # 遍历filepath下所有文件，包括子目录
    for i in files:
        fi_d = os.path.join (filepath,i) # filepath （主目录路径）+ i（子文件名称）= 子文件路径
        file = i.lower()
        if 'hp' in file or 'huawei' in file or 'dell' in file or 'lenovo' in file:
            pass
        else:
            other.append (('其他驱动',i,fi_d))
    
def main (list):
    from atl.models import Category,Atl
    for l in list:
        Category.objects.get_or_create (name = l[0])
        cate = Category.objects.get (name = l[0])
        name = l[1]
        link = str(l[2])
        Atl.objects.get_or_create (class_name = cate,name = name,link = link) # 防止导入数据重复
    
    
if __name__ == '__main__':
    find_list (filepath)
    find_other (filepath)
    main (list)
    main (other)
    print ('OK')
    